<template>
  <views class="article_Detail">
    <div class="article_Detail_flex">
      <viewTitle :title="'编辑列表'"></viewTitle>
      <div class="content">
        <el-form ref="form" label-width="auto">
          <div class="content_top">
            <el-form-item label="标题:">
              <el-input type="text" clearable placeholder="请输入标题" v-model="headline"  :disabled="type==1?true:false"></el-input>
            </el-form-item>
            <el-form-item label="颁布单位:">
              <el-input type="text" clearable placeholder="请输入颁布单位" v-model="unit" :disabled="type==1?true:false"></el-input>
            </el-form-item>
            <el-form-item label="颁布时间:">
              <el-date-picker
                v-model="time"
                type="date"
                placeholder="选择日期"
                :disabled="type==1?true:false"
              ></el-date-picker>
            </el-form-item>
          </div>
          <div class="content_bottom">
            <el-form-item label="正文:" :style="{height:300+'px'}">
              <quill-editor
                class="editor"
                v-model="content"
                ref="myQuillEditor"
                :options="editorOption"
                @blur="onEditorBlur($event)"
                @focus="onEditorFocus($event)"
                @change="onEditorChange($event)"
                :style="{height:250+'px'}"
                :disabled="type==1?true:false"
              ></quill-editor>
            </el-form-item>
          </div>
              <el-form-item label="" :style="{height:25+'px'}">
              </el-form-item>
            <el-form-item label="" v-if="type==2||type==3">
                 <el-button type="primary" @click="publishArticle" v-if="type==3">发布</el-button>
                 <el-button type="" @click="reset">重置</el-button>
                 <el-button type="warning" @click="saveArticle">{{type==2?'保存':'暂存'}}</el-button>
            </el-form-item>    
            <el-form-item label="" v-if="type==1">
                 <el-button type="primary" @click="$router.go(-1)">返回</el-button>
            </el-form-item>                     
        </el-form>
      </div>
    </div>
    <el-upload
            v-show="false"
            id="quill-upload"
            drag
            action="https://jsonplaceholder.typicode.com/posts/"
            :httpRequest="uploadFiles"
            :show-file-list="false"
    >
    </el-upload>
  </views>
</template>

<script>
  import  {mapGetters}  from 'vuex'
// 工具栏配置
const toolbarOptions = [
  ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
  //   ["blockquote", "code-block"], // 引用  代码块
  [{ header: 1 }, { header: 2 }], // 1、2 级标题
  [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
  //   [{ script: "sub" }, { script: "super" }], // 上标/下标
  [{ indent: "-1" }, { indent: "+1" }], // 缩进
  // [{'direction': 'rtl'}],                         // 文本方向
  [{ size: ["small", false, "large", "huge"] }], // 字体大小
  [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
  //   [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
  [{ color: [] }], // 字体颜色、字体背景颜色
  //   [{ font: [] }], // 字体种类
  [{ align: [] }], // 对齐方式
  ["clean"], // 清除文本格式
  ["image"] // 链接、图片、视频
];

export default {
  data() {
    return {
      type: 1, //  1详情 2修改 3新增
      headline: "", // 标题
      second_headline: "", // 副标题
      unit: "", // 单位
      time: "", // 时间
      content: "", // 富文本
      editorOption: {
        theme: "snow", // or 'bubble'
        placeholder: "您想说点什么？",
        modules: {
          toolbar: {
            container: toolbarOptions,
            // container: "#toolbar",
            handlers: {
              image: function(value) {
                if (value) {
                  // 触发input框选择图片文件
                  document.querySelector('#quill-upload input').click()
                } else {
                  this.quill.format("image", false);
                }
              }
            }
          }
        }
      }
    };
  },
  methods: {
    // 失去焦点
    onEditorBlur() {},
    // 聚焦
    onEditorFocus() {},
    // 改变
    onEditorChange() {},
    //获取数据
    getData(){
        let post_data={
          article_id:this.article_id
        }
        this.$raxios.post(
            this.$mapapi.admin_detailArticle,
            {data:post_data}
        ).then(res=>{
          if(res.code==200){
            this.headline = res.data.title;
            // this.second_headline = res.data.sub_title;
            this.unit = res.data.publish_unit;
            this.time = res.data.publish_date;
            this.content = res.data.content_preview;
          }else{
            this.tableList = [];
            this.$message.error(res.msg);
            return;
          }
        })
    },
    saveArticle(){
         this.$raxios.post(
            this.$mapapi.admin_saveArticle,{
            data:this.post_data
            }).then(res=>{
              if(res.code==200){
                this.article_id = res.data.article_id;
                this.$message({type: "success", message: "保存成功!"});
                setTimeout(()=>{this.$router.go(-1);},400)
              }else{
                  this.tableList = [];
            this.$message.error(res.msg);
            return;
              }
            })
    },
    publishArticle(){
        
         if(!this.article_id)  return this.$message({type: "error", message: "请先暂存文章!"});
        this.$raxios.post(
            this.$mapapi.admin_publishArticle,
            { data:this.post_data}
        ).then(res=>{
              if(res.code==200){
                 this.$message({type: "success", message: "发布成功!"});
                setTimeout(()=>{
                    this.$router.go(-1);
                },400)
              }else{
                  this.tableList = [];
            this.$message.error(res.msg);
            return;
              }
            }).catch(res=>{

        })  
    },
    reset(){
        this.headline = "", // 标题
        // this.second_headline = "", // 副标题
        this.unit = "", // 单位
        this.time = "", // 时间
        this.content = "" // 富文本
    },
    legalCardProsSuccess(res){
      let quill = this.$refs.myQuillEditor.quill
      if (res.code === 200) {
        let length = quill.getSelection().index;
        // 插入图片 response.data.url为服务器返回的图片地址
        quill.insertEmbed(length, 'image', res.data.url)
        // 调整光标到最后
        quill.setSelection(length + 1)
      }else{
        this.$message.error('图片插入失败')
      }
    },
	  uploadFiles(params){
		  let formData=new FormData();
		  formData.append("operator", 'goods');
		  formData.append("file", params.file);
		  this.$raxios.post(this.$mapapi.uploadFile, formData).then(res=>{
			  if(res.code==200){
				  this.$message.success(res.msg);
				  this.legalCardProsSuccess(res)
			  }else{
				  this.$message.error(res.msg);
			  }
		  }).catch(res=>{

		  })
	  }
  },
  computed:{
    ...mapGetters(['uploadFile']),
      post_data(){
          let data = {};
          data.title = this.headline;
          // data.sub_title = this.second_headline;
          data.publish_unit = this.unit;
          data.publish_date = this.time;
          data.content = this.content;
          if(this.article_id){
              data.article_id = this.article_id;
          }
          return data;
      }
  },
  created() {},
  mounted() {
        this.type=this.$route.query.type;
        this.article_id=this.$route.query.article_id||0;
        this.article_id && this.getData();
  }
};
</script>

<style lang="less"  :scope="true"  src="@/style/infomation_manager/article_Detail.less" >
.speard{
  height: 20px;
}
</style>
